package com.nsession.startup;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Created by aokage
 * Administrator on
 * Date: 14-3-24.
 */
@Slf4j
@WebListener
public final class BootstrapListener implements ServletContextListener {

    public BootstrapListener() {
    }


    private ClassPathXmlApplicationContext context;

    /**
     * Start the service.
     */
    protected void startUp() throws Exception {
        context = new ClassPathXmlApplicationContext(new String[]{"spring/security-spring-context.xml"});
        context.start();
        context.registerShutdownHook();
        log.info("security service started successfully");
    }

    /**
     * Stop the service.
     */
    protected void shutDown() throws Exception {
        context.stop();
        log.info("service stopped successfully");
    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            startUp();
        } catch (Exception ex) {
            log.error("ignore interruption ");
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        try {
            shutDown();
        } catch (Exception e) {
            log.error("shutdown error : [{}]",e.getStackTrace());
        }
    }

}
